
#apache所在路径
httpdPath=/usr/sbin
#vhost文件所在路径
vhostConfigPath=/etc/apache2/sites-enabled
#系统hosts文件路径
hostsFile=/etc/hosts

hostName=$1
hostPath=$2

showHelp() {
	cat <<EOF
	请按照如下格式调用：
	addhost 域名 域名所在网站路径
EOF
}

if [ "$hostName" = "" ] ; then	
	echo '没有输入域名'
	showHelp
	exit 1
fi

if [ "$hostPath" = '' ] ; then
	echo '没有输入当前的域名对应的文件路径'
	showHelp
	exit 1
fi
if [ ! -f $httpdPath ] ; then
	echo "$httpdPath 找不到,尝试创建"
	exit 2
fi
if [ ! -f $vhostConfig ] ; then
	echo "$vhostConfig 找不到"
	exit 2
fi
if [ ! -f $hostsFile ] ; then
	echo "$hostsFile 找不到"
	exit 2
fi

if [ ! -f $hostPath ] ; then
	echo "$hostPath 找不到,尝试创建"
	mkdir -p $hostPath
fi

echo '写入vhost'
cat <<EOF >$vhostConfigPath/$hostName.conf
	 <VirtualHost *:80>
		 	ServerName $hostName
		 	DocumentRoot "$hostPath"	   
		 	ErrorLog "/var/log/apache2/$hostName-error.log"
		 	CustomLog "/var/log/apache2/$hostName-access.log" common
	 </VirtualHost>

	 <Directory "$hostPath">
		 	Options  Indexes FollowSymLinks
		 	AllowOverride All
		 	Order allow,deny
		 	Allow from all
	 </Directory>
EOF

$httpdPath/apache2ctl restart

echo 127.0.0.1 $hostName >> $hostsFile


